package com.baiye.template.utils;


import com.baiye.template.pojo.SysInfo;
import com.sun.management.OperatingSystemMXBean;
import org.springframework.stereotype.Component;
import oshi.SystemInfo;
import oshi.hardware.CentralProcessor;

import javax.annotation.Resource;
import java.lang.management.ManagementFactory;
import java.lang.management.MemoryMXBean;
import java.lang.management.MemoryUsage;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.TimeUnit;

/**
 * @author GUOZHIPENG
 * @version 1.0
 * @date 2021/8/9 16:26
 */
public class SysInfoUtil {
    public static SysInfo getInfo(){
        SysInfo sysInfo = new SysInfo();
        try {
            DecimalFormat    df   = new DecimalFormat("######0.0000");
            SystemInfo systemInfo = new SystemInfo();

            OperatingSystemMXBean osmxb = (OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean();
            MemoryMXBean memoryMXBean = ManagementFactory.getMemoryMXBean();
            // 椎内存使用情况
            MemoryUsage memoryUsage = memoryMXBean.getHeapMemoryUsage();

//            // 初始的总内存
//            long initTotalMemorySize = memoryUsage.getInit();
//            // 最大可用内存
//            long maxMemorySize = memoryUsage.getMax();
//            // 已使用的内存
//            long usedMemorySize = memoryUsage.getUsed();

            // 操作系统
            String osName = System.getProperty("os.name");
            sysInfo.setOperatingSystem(osName);
            // 总的物理内存
            String totalMemorySize = new DecimalFormat("#.##")
                    .format(osmxb.getTotalPhysicalMemorySize() / 1024.0 / 1024 / 1024) + "G";
            sysInfo.setTotalPhysicalMemory(totalMemorySize);
            // 剩余的物理内存
            String freePhysicalMemorySize = new DecimalFormat("#.##")
                    .format(osmxb.getFreePhysicalMemorySize() / 1024.0 / 1024 / 1024) + "G";
            sysInfo.setLeftPhysicalMemory(freePhysicalMemorySize);
            String format = df.format((double) osmxb.getFreePhysicalMemorySize()/osmxb.getTotalPhysicalMemorySize());
            sysInfo.setLeftMemory(Double.valueOf(format));
            sysInfo.setUseMemory(1-sysInfo.getLeftMemory());
            // 已使用的物理内存
            String usedMemory = new DecimalFormat("#.##").format(
                    (osmxb.getTotalPhysicalMemorySize() - osmxb.getFreePhysicalMemorySize()) / 1024.0 / 1024 / 1024)
                    + "G";
            sysInfo.setUsePhysicalMemory(usedMemory);
//            // 获得线程总数
//            ThreadGroup parentThread;
//            for (parentThread = Thread.currentThread().getThreadGroup(); parentThread
//                    .getParent() != null; parentThread = parentThread.getParent()) {
//
//            }

//            int totalThread = parentThread.activeCount();

            // 磁盘使用情况
//            File[] files = File.listRoots();
//            for (File file : files) {
//                String total = new DecimalFormat("#.#").format(file.getTotalSpace() * 1.0 / 1024 / 1024 / 1024)
//                        + "G";
//                String free = new DecimalFormat("#.#").format(file.getFreeSpace() * 1.0 / 1024 / 1024 / 1024) + "G";
//                String un = new DecimalFormat("#.#").format(file.getUsableSpace() * 1.0 / 1024 / 1024 / 1024) + "G";
//                String path = file.getPath();
//                //System.err.println(path + "总:" + total + ",可用空间:" + un + ",空闲空间:" + free);
//                //System.err.println("=============================================");
//            }

          //  System.err.println("操作系统:" + osName);
//            System.err.println("程序启动时间:" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
//                    .format(new Date(ManagementFactory.getRuntimeMXBean().getStartTime())));
            sysInfo.setStartTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
                    .format(new Date(ManagementFactory.getRuntimeMXBean().getStartTime())));
            //System.err.println("pid:" + System.getProperty("PID"));
            //System.err.println("cpu核数:" + Runtime.getRuntime().availableProcessors());
            //sysInfo = printlnCpuInfo(systemInfo);
            CentralProcessor processor = systemInfo.getHardware().getProcessor();
            long[] prevTicks = processor.getSystemCpuLoadTicks();
            // 睡眠1s
            TimeUnit.SECONDS.sleep(1);
            long[] ticks = processor.getSystemCpuLoadTicks();
            long nice = ticks[CentralProcessor.TickType.NICE.getIndex()]
                    - prevTicks[CentralProcessor.TickType.NICE.getIndex()];
            long irq = ticks[CentralProcessor.TickType.IRQ.getIndex()]
                    - prevTicks[CentralProcessor.TickType.IRQ.getIndex()];
            long softirq = ticks[CentralProcessor.TickType.SOFTIRQ.getIndex()]
                    - prevTicks[CentralProcessor.TickType.SOFTIRQ.getIndex()];
            long steal = ticks[CentralProcessor.TickType.STEAL.getIndex()]
                    - prevTicks[CentralProcessor.TickType.STEAL.getIndex()];
            long cSys = ticks[CentralProcessor.TickType.SYSTEM.getIndex()]
                    - prevTicks[CentralProcessor.TickType.SYSTEM.getIndex()];
            long user = ticks[CentralProcessor.TickType.USER.getIndex()]
                    - prevTicks[CentralProcessor.TickType.USER.getIndex()];
            long iowait = ticks[CentralProcessor.TickType.IOWAIT.getIndex()]
                    - prevTicks[CentralProcessor.TickType.IOWAIT.getIndex()];
            long idle = ticks[CentralProcessor.TickType.IDLE.getIndex()]
                    - prevTicks[CentralProcessor.TickType.IDLE.getIndex()];
            long totalCpu = user + nice + cSys + idle + iowait + irq + softirq + steal;
            //System.err.println("cpu核数:" + processor.getLogicalProcessorCount());
            sysInfo.setCpuNum(processor.getLogicalProcessorCount());

            double sysUse =cSys * 1.0 / totalCpu;
            sysUse=Double.valueOf(df.format(sysUse));
            double userUse = user * 1.0 / totalCpu;
            String format2 = df.format(userUse);
            userUse=Double.valueOf(format2);
            //System.err.println("cpu当前等待率:" + new DecimalFormat("#.##%").format(iowait * 1.0 / totalCpu));
            sysInfo.setCpuWaitingRate(iowait * 1.0 / totalCpu);
           // System.err.println("cpu当前空闲率:" + new DecimalFormat("#.##%").format(idle * 1.0 / totalCpu));
            sysInfo.setIdleRate(Double.valueOf(df.format(idle * 1.0 / totalCpu)));
            //System.err.format("CPU load: %.1f%% (counting ticks)%n", processor.getSystemCpuLoadBetweenTicks() * 100);
            //System.err.format("CPU load: %.1f%% (OS MXBean)%n", processor.getSystemCpuLoad() * 100);

            sysInfo.setSysCpuUse(sysUse);
            sysInfo.setUserCpuUse(userUse);
            double left = 1-sysUse-userUse;
            String format3 = df.format(left);
            left=Double.valueOf(format3);
            sysInfo.setLeftCpu(left);





            //System.err.println("JAVA_HOME:" + System.getProperty("java.home"));
            sysInfo.setJavaHome(System.getProperty("java.home"));
            //System.err.println("JAVA_VERSION:" + System.getProperty("java.version"));
            sysInfo.setJavaVersion(System.getProperty("java.version"));
            //System.err.println("USER_HOME:" + System.getProperty("user.home"));
            //System.err.println("USER_NAME:" + System.getProperty("user.name"));
//            System.err.println("初始的总内存(JVM):"
//                    + new DecimalFormat("#.#").format(initTotalMemorySize * 1.0 / 1024 / 1024) + "M");
//
//            System.err.println(
//                    "最大可用内存(JVM):" + new DecimalFormat("#.#").format(maxMemorySize * 1.0 / 1024 / 1024) + "M");
//            System.err.println(
////                    "已使用的内存(JVM):" + new DecimalFormat("#.#").format(usedMemorySize * 1.0 / 1024 / 1024) + "M");
//            System.err.println("总的物理内存:" + totalMemorySize);
//            System.err
//                    .println("总的物理内存:"
//                            + new DecimalFormat("#.##").format(
//                            systemInfo.getHardware().getMemory().getTotal() * 1.0 / 1024 / 1024 / 1024)
//                            + "M");
//            System.err.println("剩余的物理内存:" + freePhysicalMemorySize);
//            System.err
//                    .println("剩余的物理内存:"
//                            + new DecimalFormat("#.##").format(
//                            systemInfo.getHardware().getMemory().getAvailable() * 1.0 / 1024 / 1024 / 1024)
//                            + "M");
//            System.err.println("已使用的物理内存:" + usedMemory);
//            System.err.println("已使用的物理内存:"
//                    + new DecimalFormat("#.##").format((systemInfo.getHardware().getMemory().getTotal()
//                    - systemInfo.getHardware().getMemory().getAvailable()) * 1.0 / 1024 / 1024 / 1024)
//                    + "M");
//            System.err.println("总线程数:" + totalThread);
//            System.err.println("===========================");
            return sysInfo;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return sysInfo;
    }

    private static SysInfo printlnCpuInfo(SystemInfo systemInfo) throws InterruptedException {
        CentralProcessor processor = systemInfo.getHardware().getProcessor();
        long[] prevTicks = processor.getSystemCpuLoadTicks();
        // 睡眠1s
        TimeUnit.SECONDS.sleep(1);
        long[] ticks = processor.getSystemCpuLoadTicks();
        long nice = ticks[CentralProcessor.TickType.NICE.getIndex()]
                - prevTicks[CentralProcessor.TickType.NICE.getIndex()];
        long irq = ticks[CentralProcessor.TickType.IRQ.getIndex()]
                - prevTicks[CentralProcessor.TickType.IRQ.getIndex()];
        long softirq = ticks[CentralProcessor.TickType.SOFTIRQ.getIndex()]
                - prevTicks[CentralProcessor.TickType.SOFTIRQ.getIndex()];
        long steal = ticks[CentralProcessor.TickType.STEAL.getIndex()]
                - prevTicks[CentralProcessor.TickType.STEAL.getIndex()];
        long cSys = ticks[CentralProcessor.TickType.SYSTEM.getIndex()]
                - prevTicks[CentralProcessor.TickType.SYSTEM.getIndex()];
        long user = ticks[CentralProcessor.TickType.USER.getIndex()]
                - prevTicks[CentralProcessor.TickType.USER.getIndex()];
        long iowait = ticks[CentralProcessor.TickType.IOWAIT.getIndex()]
                - prevTicks[CentralProcessor.TickType.IOWAIT.getIndex()];
        long idle = ticks[CentralProcessor.TickType.IDLE.getIndex()]
                - prevTicks[CentralProcessor.TickType.IDLE.getIndex()];
        long totalCpu = user + nice + cSys + idle + iowait + irq + softirq + steal;
        System.err.println("cpu核数:" + processor.getLogicalProcessorCount());
        DecimalFormat    df   = new DecimalFormat("######0.0000");
        double sysUse =cSys * 1.0 / totalCpu;
        String format = df.format(sysUse);
        sysUse=Double.valueOf(format);
        double userUse = user * 1.0 / totalCpu;
        String format2 = df.format(userUse);
        userUse=Double.valueOf(format2);
        System.err.println("cpu当前等待率:" + new DecimalFormat("#.##%").format(iowait * 1.0 / totalCpu));

        System.err.println("cpu当前空闲率:" + new DecimalFormat("#.##%").format(idle * 1.0 / totalCpu));
        System.err.format("CPU load: %.1f%% (counting ticks)%n", processor.getSystemCpuLoadBetweenTicks() * 100);
        System.err.format("CPU load: %.1f%% (OS MXBean)%n", processor.getSystemCpuLoad() * 100);
        SysInfo sysInfo = new SysInfo();
        sysInfo.setSysCpuUse(sysUse);
        sysInfo.setUserCpuUse(userUse);
        double left = 1-sysUse-userUse;
        String format3 = df.format(left);
        left=Double.valueOf(format3);
        sysInfo.setLeftCpu(left);

        return sysInfo;
    }

    public static void main(String[] args) {
        SysInfo infp = SysInfoUtil.getInfo();
        System.out.println(infp.toString());
    }
}
